<template>
	<view>
		<view class="upic" @click="previewImg(ypic)">
			<u-image width="500rpx" mode="widthFix" :src="goodsdetail.cover_url"></u-image>
		</view>
		
		
		<u-cell-group>
			<u-cell-item  :title="goodsdetail.title" :value="'库存:'+goodsdetail.stock" hover-class="none" :arrow="false" ></u-cell-item>
			<u-cell-item  :title="'￥ '+goodsdetail.price+'.00'" :value="'销量:'+goodsdetail.sales" hover-class="none" :arrow="false"></u-cell-item>
		</u-cell-group>
		<u-tabs name="cate_name" count="cate_count" :list="list" :is-scroll="false" :current="current" @change="change" active-color="#ff9900"></u-tabs>
		<view v-if="current == 0">
			<view class="u-p-10 u-m-b-60">
				<u-parse :html="goodsdetail.details" :selectable="true"></u-parse>
			</view>
		</view>
		<view v-if="current == 1" >
			<u-row gutter="16">
				<u-col span="6" v-for="(goods, index) in goodsList" :key="index">
					<!-- 使用组件 -->
						<goods-card :goods="goods" @myClick="toPage"></goods-card>
				</u-col>
			</u-row>
		</view>
		<view v-if="current == 2">
			<view class="u-p-30 u-m-b-60" v-for="(res, index)  in commentList" :key="res.id">
				{{res.user.name}}
				{{res.content}}
			</view>
		</view>
		
		<view class="navigation">
			<view class="left">
				<view class="item" @click="goTohome">
					<u-icon name="home" :size="40" :color="$u.color['contentColor']"></u-icon>
					<view class="text u-line-1">首页</view>
				</view>
				<view class="item" @click="goTosc">
					<view class="" v-if="isCollect === 0">
						<u-icon name="bag" :size="40" :color="$u.color['contentColor']"></u-icon>
						<view class="text u-line-1">收藏</view>
					</view>
					<view class="" v-else>
						<u-icon name="bag" :size="40" color="red"></u-icon>
						<view class="text u-line-1" style="color: red;">收藏</view>
					</view>
				</view>
				<view class="item car"  @click="toCart">
					<u-badge class="car-num" :count="cartCount" type="error" :offset="[-3, -6]"></u-badge>
					<u-icon name="shopping-cart" :size="40" :color="$u.color['contentColor']"></u-icon>
					<view class="text u-line-1">购物车</view>
				</view>
			</view>
			<view class="right">
				<view class="cart btn u-line-1"  @click="addCartgood">加入购物车</view>
				<view class="buy btn u-line-1" @click="goCart">立即购买</view>
			</view>
		</view>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [{
					cate_name: '商品详情'
				}, {
					cate_name: '相关商品'
				}, {
					cate_name: '商品评价',
					cate_count:0
				}],
				current: 0,
				goodsdetail:[],
				commentList:[],
				goodsList:[],
				goodsId:null,
				cartCount:0,
				isCollect:0,
				num:0,
				ypic:''
			}
		},
		onLoad(option) {   //onLoad页面加载
			this.goodsId = option.id
			this.getDate()  //商品详情
			this.getCartCount() //购物车数量
			this.init()
		},
		methods: {
			async getDate(){
				const res = await this.$u.api.goodsInfo(this.goodsId)
				// console.log(res)
				this.goodsdetail = res.goods
				this.goodsList = res.like_goods
				this.commentList = res.goods.comments
				this.list[2].cate_count = res.goods.comments.length
				this.isCollect = res.goods.is_collect
				this.ypic = res.goods.cover_url
			},
			change(index) {
				this.current = index;
			},
			//查找购物车里此商品数量
	        async init(){
				if(this.vuex_token){
					const res = await this.$u.api.cartList()
					this.goodsProducts = res.data
					this.num = res.data.filter(n=>n.goods_id == this.goodsId).map(n => n.num)[0] + 1;
				}
				
			},
			//加入购物车
			async addCartgood() {
				//查找购物车里此商品数量
				this.init()
				console.log(this.num)
				const params = {
					goods_id:this.goodsId
				}
				if(this.goodsdetail.stock == 0){
					this.$u.toast('没有库存了')
				}else if(this.num >= this.goodsdetail.stock){
					this.$u.toast('购买数量大于库存')
				}else if(this.goodsdetail.stock > 0  ){
					await this.$u.api.addCart(params)
					this.$u.toast('成功添加购物车')
				}
						
				//调用 获取购物车数量 函数
				this.getCartCount()
			},
			//立刻购买
			async goCart() {
				this.init()
				const params = {
					goods_id:this.goodsId
				}
				if(this.goodsdetail.stock == 0){
					this.$u.toast('没有库存了')
				}else if(this.num >= this.goodsdetail.stock){
					this.$u.toast('购买数量大于库存')
				}else if(this.goodsdetail.stock > 0){
					await this.$u.api.addCart(params)
					this.$u.toast('成功添加购物车')
					setTimeout(()=>{
						this.$u.route({
							type:'switchTab',    //switchTab 跳转到tabbar
							url:'/pages/cart/cart'
						})
					},1000)
				}
						
				//调用 获取购物车数量 函数
				this.getCartCount()
			},
			//获取购物车数量
			async getCartCount(){
				const token = this.vuex_token
				if(token) {
					const res = await this.$u.api.cartList()
					this.cartCount = res.data.length
				}
			},
			//跳转首页
			goTohome(){
				this.$u.route({
					type:'switchTab',    //switchTab 跳转到tabbar
					url:'/pages/index/index'
				})
			},
			//跳转到购物车
			toCart(){
				this.$u.route({
					type:'switchTab',    //switchTab 跳转到tabbar
					url:'/pages/cart/cart'
				})
			},
			//收藏
			async goTosc(){
				await this.$u.api.goodsSc(this.goodsId)
				
				if(this.isCollect == 0){
					this.$u.toast('收藏成功')
					this.isCollect = 1
				}else{
					this.$u.toast('取消收藏')
					this.isCollect = 0
				}
			},
			//点击查看原图
			previewImg(ypic) {
				let _this = this;
				let imgsArray = [];
				imgsArray[0] = ypic
				uni.previewImage({
					current: 0,
					urls: imgsArray
				});
			},
			//子组件需要跳转的页面
			toPage(id){
				this.$u.route({
					type:'redirectTo',  
					url:`/pages/cate/goodsdetail?id=${id}`
				})
			}
			//点击看多图
			// photoClick(index) {
			// 	let arr = [];
			// 	arr.push(this.rootUrl + this.dataList[index].ImgPath);
			// 	//uniapp预览图片
			// 	uni.previewImage({
			// 		current: index, //预览图片的下标
			// 		urls: arr //预览图片的地址，必须要数组形式，如果不是数组形式就转换成数组形式就可以
			// 	});
			// }
			

			
		}
	}
</script>

<style lang="scss" scoped>
.navigation {
	width: 100%;
	display: flex;
	position: fixed;
	margin-top: 100rpx;
	bottom: 0;
	border: solid 2rpx #f2f2f2;
	background-color: #ffffff;
	padding: 16rpx 0;
	.left {
		display: flex;
		font-size: 20rpx;
		.item {
			margin: 0 30rpx;
			&.car {
				text-align: center;
				position: relative;
				.car-num {
					position: absolute;
					top: -10rpx;
					right: -10rpx;
				}
			}
		}
	}
	.right {
		display: flex;
		font-size: 28rpx;
		align-items: center;
		.btn {
			line-height: 66rpx;
			padding: 0 30rpx;
			border-radius: 36rpx;
			color: #ffffff;
		}
		.cart {
			background-color: #ed3f14;
			margin-right: 30rpx;
		}
		.buy {
			background-color: #ff7900;
		}
	}
}
.upic {
	justify-content: center;
	display: flex;
	flex-wrap: wrap;
	flex-direction: row;
	align-items: center;
}
</style>

